<?php
require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/daoQuick_Message.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busUser.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/utility/sendMail.php';

class Quick_MessageBus {
	private $_userBus;
	private $_quick_MessageDao;
	
	public function __construct() {
		$this->_quick_MessageDao = new Quick_MessageDao();
		$this->_userBus = new UserBus();
	}
	
	public function addQuick_Message($quick_message) {
		global $ubi;
		
		$quick_message->setContent(GeneralFunctions::stripString($quick_message->getContent()));
		
		$currentTime = $_SERVER['REQUEST_TIME'];
		$quick_message->setCreatedDate($currentTime);

		$id = $this->_quick_MessageDao->addQuick_Message($quick_message);
		if ($id) {
			// add score for the person who sends quick message
			global $ubi_score;
			$this->_userBus->addScore($quick_message->getSenderID(), $ubi_score['SEND_QUICK_MESSAGE']);
			
			// send email for the owner wall
			$receiver = $this->_userBus->getUser($quick_message->getReceiverID());
			if (!in_array($ubi['EMAIL']['WRITE_ON_MY_WALL'], $receiver->getEmailPrivacies())) {
				$sender = $this->_userBus->getUser($quick_message->getSenderID());
				
				SendMail::getEmailTemplate($ubi['EMAIL']['WRITE_ON_MY_WALL'], $template, $subject);
				
				$arrParam = array();
				$arrParam[] = $receiver->getFirstName() . ' ' . $receiver->getLastName();	
				$arrParam[] = '<a href="' . $ubi['SERVER']['SITE_ROOT'] . 'index.php?view&type=user&userid=' . $sender->getUserID() . '">' . $sender->getUserName() . '</a>';
				$arrParam[] = $quick_message->getContent();
				$arrParam[] = '<a href="' . $ubi['SERVER']['SITE_ROOT'] . '">' . $ubi['SERVER']['SITE_NAME'] . '</a>';
				
				SendMail::sendEMail($template, $subject, $arrParam, $receiver->getEmail());
			}
		}
		return $id;
	}
	
	public function getQuick_MessagesBySenderId($senderID) {
		return $this->_quick_MessageDao->getQuick_MessagesBySenderId($senderID);
	}

	public function getTopQuick_MessagesByReceiverId($receiverID) {
		global $ubi;
		$nMessages = $ubi['QUICK_MESSAGE']['DEFAULT'];
		return $this->_quick_MessageDao->getQuick_MessagesByReceiverId($receiverID, 0, $nMessages);
	}
	
	public function getQuick_MessagesByReceiverId($receiverID) {		
		return $this->_quick_MessageDao->getQuick_MessagesByReceiverId($receiverID);
	}
		
	public function removeQuickMessage($messageID) {
		return $this->_quick_MessageDao->removeQuickMessage($messageID);
	}
	
	public function countQuickMessage($receiverID) {
		return $this->_quick_MessageDao->countQuickMessage($receiverID);
	}
	
	public function getQuickMessagesOfPage($receiverID, $page) {
		global $ubi;
		
		$from = ($page - 1) * $ubi['QUICK_MESSAGE']['MESSAGE_PER_PAGE'];
		$to = $from +  $ubi['QUICK_MESSAGE']['MESSAGE_PER_PAGE'];
		
		return $this->_quick_MessageDao->getQuick_MessagesByReceiverId($receiverID, $from, $to);
	}
	
	public function getSendersFromQuickMessage($messgages) {
		$users = array();
		
		foreach ($messgages as $mess) {
			$user = $this->_userBus->getUser($mess->getSenderID());
			$users[$mess->getSenderID()] = $user;
		}
		
		return $users;
	}
}
?>